package cn.dogger.easyboot.init.service.impl;

import cn.dogger.easyboot.init.domain.TableEntity;
import cn.dogger.easyboot.init.handler.FieldHandler;
import cn.dogger.easyboot.init.mapper.InitTableMapper;
import cn.dogger.easyboot.init.service.InitTableService;
import org.apache.ibatis.logging.Log;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 初始化表Service
 * @author dogger
 * 2022/7/19 21:53
 */
@Service
public class InitTableServiceImpl implements InitTableService {

    private final InitTableMapper mapper;

    public InitTableServiceImpl(InitTableMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public void initTable(){
        List<TableEntity> entityAnnotationValues = FieldHandler.getEntityAnnotationValues();
        entityAnnotationValues.forEach(tb ->{
            tb = FieldHandler.rationalizationSql(tb);
            //判断是否已经存在表,如果存在,就编辑表,不存在就创建
           Boolean tableExistFlag =  mapper.tableExistFlag(tb.getName());
            if(tableExistFlag){
                System.out.println(tb.getRemark()+"已存在");
               //编辑表字段
            }else{
                System.out.println("正在创建"+tb.getRemark());
                mapper.createTable(tb);
            }
        });
    }



}
